// 开启严格模式，注意：在严格模式下，this如果指向undefined，就说明事项window
'use strict'
// 导入node.ks.path模块
const path = require( 'path' )
// 导入src下的settings.js的配置
const defaultSettings = require( './src/settings.js' )
// 将相对路径转为绝对路径
function resolve ( dir ) {
  return path.join( __dirname, dir )
}
// 定义网页的标题，如果defaultSettings模块下没有等于标题就使用'vue Admin Template'
const name = defaultSettings.title || 'vue Admin Template'

// 设置项目运行的端口（读取环境变量里面的prot，如果没有就读取packagejson里面的port，如果还没有就使用9528）
const port = process.env.port || process.env.npm_config_port || 9528 // dev port

// 定义对象形式配置 https://cli.vuejs.org/config/
module.exports = {
  // 资源应用路径
  publicPath: '/',
  // 打包输出目录
  outputDir: 'dist',
  // 打包静态文件目录
  assetsDir: 'static',
  // 是否开启代码校验
  lintOnSave: process.env.NODE_ENV === 'development',
  // 是否关闭map文件
  productionSourceMap: false,
  // 开发服务器介绍
  devServer: {

    // 端口
    port: port,
    // 是否自动打开浏览器
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    // 代理跨域的配置
    proxy: {
      // 当我们的本地的请求 有/api的时候，就会代理我们的请求地址向另外一个服务器发出请求
      '/api': {
        target: 'http://ihrm.itheima.net/', // 跨域请求的地址
        changeOrigin: true // 只有这个值为true的情况下 才表示开启跨域
      }
    },
    // before: require('./mock/mock-server.js')
  },

  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve( 'src' )
      }
    }
  },
  chainWebpack ( config ) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin( 'preload' ).tap( () => [
      {
        rel: 'preload',
        // to ignore runtime.js
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [ /\.map$/, /hot-update\.js$/, /runtime\..*\.js$/ ],
        include: 'initial'
      }
    ] )

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete( 'prefetch' )
    // 定义函数形式配置
    // set svg-sprite-loader
    config.module
      .rule( 'svg' )
      .exclude.add( resolve( 'src/icons' ) )
      .end()
    config.module
      .rule( 'icons' )
      .test( /\.svg$/ )
      .include.add( resolve( 'src/icons' ) )
      .end()
      .use( 'svg-sprite-loader' )
      .loader( 'svg-sprite-loader' )
      .options( {
        symbolId: 'icon-[name]'
      } )
      .end()

    config
      .when( process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin( 'ScriptExtHtmlWebpackPlugin' )
            .after( 'html' )
            .use( 'script-ext-html-webpack-plugin', [ {
              // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            } ] )
            .end()
          config
            .optimization.splitChunks( {
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve( 'src/components' ), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            } )
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          config.optimization.runtimeChunk( 'single' )
        }
      )
  }
}
